from kgforge.core import KnowledgeGraphForge
A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook 00-Initialization.ipynb.
Note: DemoStore doesn't implement file operations yet. Use the BluBrainNexus store instead when creating a config file.
forge = KnowledgeGraphForge("../../configurations/forge.yml")
from kgforge.core import Resource
from kgforge.specializations.resources import Dataset
import pandas as pd
! ls -p ../../data | egrep -v /$
persons = Dataset(forge, name="Interesting Persons")
persons.add_files("../../data/persons.csv")
forge.register(persons)
forge.as_json(persons)
associations = Dataset(forge, name="Associations data")
associations.add_files("../../data/associations.tsv")
associations.add_derivation(persons)
forge.register(associations)
forge.as_json(associations)
# By default the files are downloaded in the current path (path="."). The urls or the files to download can be collected from a different json path (by setting a value for "follow") and
# the files downloaded to a different path (by setting a value for "path")
# The argument overwrite: bool can be provided to decide whether to overwrite (True) existing files with the same name or
# to create new ones (False) with their names suffixed with a timestamp.
# A cross_bucket argument can be provided to download data from the configured bucket (cross_bucket=False - the default value)
# or from a bucket different than the configured one (cross_bucket=True). The configured store should support crossing buckets for this to work.
associations.download(source="parts")
# A specific path can be provided.
associations.download(path="./downloaded/", source="parts")
# A specific content type can be downloded.
associations.download(path="./downloaded/", source="parts", content_type="text/tab-separated-values")
! ls -l ./downloaded
# ! rm -R ./downloaded/
persons = Dataset(forge, name="Interesting Persons")
persons.add_distribution("../../data/associations.tsv")
persons.add_image(path='../../data/non_existing_person.jpg', content_type='application/jpeg', about='Person')
forge.register(persons)
forge.as_json(persons)
# When files are added as distributions, they can be directly downloaded without specifying which json path to use to collect the downlodable urls. In addition, content type and path arguments
# can still be provided
persons.download()
distribution_1 = forge.attach("../../data/associations.tsv")
distribution_2 = forge.attach("../../data/persons.csv")
jane = Resource(type="Person", name="Jane Doe", distribution=distribution_1)
john = Resource(type="Person", name="John Smith", distribution=distribution_2)
persons = [jane, john]
forge.register(persons)
dataset = Dataset(forge, name="Interesting people")
dataset.add_parts(persons)
forge.register(dataset)
forge.as_json(dataset)
dataset.download(path="./downloaded/", source="parts")
! ls -l ./downloaded
# ! rm -R ./downloaded/
dataset = Dataset.from_resource(forge, [jane, john], store_metadata=True)
print(*dataset, sep="\n")
See notebook 07 DataFrame IO.ipynb
for details on conversions of instances of Resource from a Pandas DataFrame.
dataframe = pd.read_csv("../../data/persons.csv")
dataframe
persons = forge.from_dataframe(dataframe)
forge.register(persons)
dataset = Dataset(forge, name="Interesting people")
dataset.add_parts(persons)
forge.register(dataset)
forge.as_json(dataset)
dataframe = pd.read_csv("../../data/associations.tsv", sep="\t")
dataframe
dataframe["distribution"] = dataframe["distribution"].map(lambda x: forge.attach(x))
associations = forge.from_dataframe(dataframe, na="(missing)", nesting="__")
print(*associations, sep="\n")
forge.register(associations)
dataset = Dataset(forge, name="Interesting associations")
dataset.add_parts(associations)
forge.register(dataset)
forge.as_json(dataset)